package com.suluyun.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.suluyun.R;
import com.suluyun.fragment.MainListFragment;
import com.suluyun.fragment.MainMineFragemnt;
import com.suluyun.fragment.MainPageFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button tabMain;
    private Button tabTemplet;
    private Button tabMine;

    private FrameLayout ly_content;
    private MainListFragment mainListFragment;
    private MainMineFragemnt mainMineFragemnt;
    private MainPageFragment mainPageFragment;
    private TextView tvMainTitle;

    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_page);
        bindView();
    }

    private void bindView(){
        tabMain = (Button)findViewById(R.id.BottomBtnMain);
        tabTemplet = (Button)findViewById(R.id.BottomBtnMod);
        tabMine = (Button)findViewById(R.id.BottomBtnMine);
        ly_content = (FrameLayout) findViewById(R.id.fragment_container);
        tvMainTitle = (TextView)findViewById(R.id.tvMainTitle);

        tabMain.setOnClickListener(this);
        tabTemplet.setOnClickListener(this);
        tabMine.setOnClickListener(this);

        tabMine.performClick();//自动执行点击事件
    }

    public void hideAllFragment(FragmentTransaction transaction){
        if(mainPageFragment!=null){
            transaction.hide(mainPageFragment);
        }
        if(mainListFragment!=null){
            transaction.hide(mainListFragment);
        }
        if(mainMineFragemnt!=null){
            transaction.hide(mainMineFragemnt);
        }
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (v.getId()){
            case R.id.BottomBtnMain:
                tvMainTitle.setText(tabMain.getText());
                if (mainPageFragment==null){
                    mainPageFragment = MainPageFragment.createInstance();
                    transaction.add(R.id.fragment_container,mainPageFragment);
                }else{
                    transaction.show(mainPageFragment);
                }
                break;
            case R.id.BottomBtnMod:
                tvMainTitle.setText(tabTemplet.getText());
                if(mainListFragment == null){
                    mainListFragment = MainListFragment.createInstance();
                    transaction.add(R.id.fragment_container,mainListFragment);
                }else{
                    transaction.show(mainListFragment);
                }
                break;
            case R.id.BottomBtnMine:
                tvMainTitle.setText(tabMine.getText());
                if(mainMineFragemnt == null){
                    mainMineFragemnt = MainMineFragemnt.createInstance();
                    transaction.add(R.id.fragment_container,mainMineFragemnt);
                }else{
                    transaction.show(mainMineFragemnt);
                }
                break;
        }
        transaction.commit();
    }
}
